<template>
	<view class="content">
		<view class="pageHeader">
			微信绑定
			<a class="generalBack" @click="backpage()" hover-class="none">
			 <image src="/static/b-1.png" style="width: 18upx;height: 30upx;"></image>
			 </a>
		</view>
		<view class="regoodbox">
		
		<!-- 	<view class="flex aic fsb" style="padding: 200upx 0upx 2upx;font-size: 28upx;color: #666;border-bottom: 2upx solid #EFEFEF;">
				<input type="text" placeholder="请输入验证码" v-model="verify" style="font-size: 30upx;" />
				<button v-if="!is_send" @tap="sendVerify()" class="code">发送验证码</button>
				<button v-if="is_send"  class="code">{{times}}s</button>
			</view> -->
			
			<button @tap="wxbind()" class="surebtn">绑定微信</button>
		</view>
	</view>
	</template>
	<script>
	import postAjax from '../../../API/postAjax.js'
	export default {
		data() {
			return {
				userToken:'',// token
				phone:'',
				verify:'',
				times:60,
				is_send:false,
			}
		},
		onLoad(options) {
			let that = this
			that.userToken = uni.getStorageSync('token')
			that.phone=uni.getStorageSync('userinfo').mobile
		},
		methods: {
			// 返回上一页
			backpage(){
				uni.navigateBack({
					delta:1
				})
			},
			sendVerify(){
				let that = this
						
				
				
				if(!that.phone){
					uni.showToast({  title:'手机号码不存在，请先绑定手机号码',  icon:'none'})	
				}
				
				
				postAjax('user/RegVerifySend',{				
					mobile:that.phone,				
				},function(data){
															
					if(data.code==0){
						uni.showToast({
						  title:'短信发送成功',
						  icon:'none'
						})						
						
						that.getCode()
					}else{
						uni.showToast({
						  title:'短信发送失败，请稍后重试',
						  icon:'none'
						})
					}
							
				})
			},
			 // 倒计时
           	getCode: function () {
				    
					this.is_send=true
				   var interval = setInterval(() => {
					  --this.times
				   }, 1000)
				   setTimeout(() => {
					  clearInterval(interval)
					  this.is_send=false
				   }, 60000)
			},
	
			
			wxbind(){
				let that = this
				let wx_openid
				// if(!that.verify){
				// 	uni.showToast({
				// 	  title:'请输入验证码',
				// 	  icon:'none'
				// 	})	
				// 	return;
				// }
				
				uni.login({
					provider: 'weixin',
					success: function (loginRes) {
							  
						uni.getUserInfo({
							provider: 'weixin',
							success: function (infoRes) {
								//infoRes里面有用户信息需要的话可以取一下
							
								
								let openId=infoRes.userInfo.openId;
								let nickname=infoRes.userInfo.nickName;
								let city=infoRes.userInfo.city;
								let province=infoRes.userInfo.province;
								let gender=infoRes.userInfo.gender;
								let avatar=infoRes.userInfo.avatarUrl;
								let weixin_unionid=infoRes.userInfo.unionId;
								
								let formdata={
									mobile:that.phone,
									nickname:nickname,
									app_type:'weixin',
									verify:that.verify,
									weixin_openid:openId,
									city:city,
									province:province,
									gender:gender,
									avatar:avatar,
									weixin_unionid:weixin_unionid,
								};
								
								console.log(formdata)	
																	
								postAjax('user/Reg',formdata,function(data){
									console.log(data)
										let userInfo=data.data								
										if(data.code==0){
											
											uni.setStorageSync('token', userInfo.token);
											uni.setStorageSync('userinfo', userInfo);
											uni.getSystemInfo({
											    success: function (res) {						       
													uni.setStorageSync('application_client_type', res.platform);
											    },
											});
											
											uni.showToast({
											  title:'微信绑定成功',
											  icon:'none'
											})		
																
											setTimeout(function(){
												uni.switchTab({
													url:'/pages/tab/index'
												})
											},1000) 
										}else{
											uni.showToast({
											  title:'微信绑定失败，请稍后重试',
											  icon:'none'
											})
										}										
												
								})
								
							},				 
						})
					},
				})
				
			}
		}
	}
	
</script>

<style>
	.content{min-height: 100vh;background-color: #F9F9F9;}
	.pageHeader{width: 100%;height: 128upx;background-color: #1D84E8;line-height: 128upx;text-align: center;font-size: 36upx;color: #fff;padding-top: 40upx;}
	.generalBack{position: absolute;top:40upx;left:30upx;display: block;}
	.surebtn{display: block;font-size: 30upx;width:456upx;padding: 0;background:#1D84E8;color: #fff;margin: 260upx auto 0;}
	.surebtn::after,.code::after{border: none;}
	.code{color: #007AFF;font-size: 30upx;border:none;display: inline-block;width:180upx;background-color: transparent;padding: 0;}
	.regoodbox{padding:0 30upx;}
</style>
